/**
 * 2017年11月13日
 */
package exp.algorithm.sic.feature;

import java.util.ArrayList;
import java.util.List;

import exp.algorithm.sic.ScalePeak;
import exp.algorithm.sic.TimeSeries;

/**
 * 线程安全
 * @author Alex
 *
 */

public class StatisticsFeatureMaker extends SegmentedSupport{
	protected float[] getStatistics(int beginX, int length, TimeSeries ts) {
		if(length == 0 ) 
			throw new IllegalArgumentException("TimeSeries length should > 0 begin index: "+beginX);
		float[] res = new float[3];
		float sum = 0;
		for (int i = 0; i < length; i++) {
			sum += ts.data[i + beginX];
		}
		float mean = sum / length;
		sum = 0;
		for (int i = 0; i < length; i++) {
			sum += (ts.data[i + beginX] - mean) * (ts.data[i + beginX] - mean);
		}
		float stdvar = sum / length;
		float slope = (float) Math.atan((ts.data[beginX + length - 1] / ts.data[beginX]));
		res[0] = mean;
		res[1] = stdvar;
		res[2] = slope;
		return res;
	}

	@Override
	public List<FeatureVector> createFeatureDescriptor(List<ScalePeak> peaks, TimeSeries[] tss) {
		ArrayList<FeatureVector> rr = new ArrayList<>(peaks.size());
		int vecLength = PEAKNUM*3;
		for(ScalePeak s:peaks){
			int spx = s.x;
			float[]v = new float[vecLength];
			int segXs[][] = getSegmentedStartIndex(spx, tss[s.level]);
			int leftNum = segXs[1][0];
			int offset = 0;
			if(leftNum!=N){
				offset += (N-leftNum)*3;
			}
			int xs[] = segXs[0];
			for(int i = 0 ;i<xs.length;i++){
				float[]res = getStatistics(xs[i], M,tss[s.level]);
				System.arraycopy(res, 0, v, offset, res.length);
				offset+=3; //xs.length*
			}
			rr.add(new DenseFeatureVector(v));
		}
		
		return rr;
	}

}
